package os

import (
	"context"
	"encoding/json"
	"github.com/shirou/gopsutil/v3/disk"

	"os1/internal/svc"
	"os1/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DiskUsageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDiskUsageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DiskUsageLogic {
	return &DiskUsageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DiskUsageLogic) DiskUsage(req *types.RequestUrl) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	usage, err := disk.Usage(req.Url)
	if err != nil {
		return nil, err
	}
	var us []types.UsageStat
	marshal, err := json.Marshal(usage)
	var u types.UsageStat
	json.Unmarshal(marshal, &u)
	us = append(us, u)
	return &types.Response{
		UsageStats: us,
	}, nil
}
